package org.yn.car.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.yn.car.domain.CarResources;

import org.yn.car.dto.SaveCarResourceDTO;
import org.yn.car.mapper.CarResourcesMapper;
import org.yn.car.service.ICarResourcesService;
import org.yn.base.service.impl.BaseServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yn
 * @since 2023-08-21
 */
@Service
public class CarResourcesServiceImpl extends BaseServiceImpl<CarResources> implements ICarResourcesService {

    @Autowired
    CarResourcesMapper carResourcesMapper;

    /**
     * 先根据车辆ID先全部删除,再批量新增
     * @param dto
     */
    @Transactional
    @Override
    public void batchSaveCarResources(SaveCarResourceDTO dto) {
        //先根据车辆id删除表中所对应的所有 资源
        carResourcesMapper.deleteByCarId(dto.getCarId());

        // 获取传入的list
        List<CarResources> list = dto.getList();

        //批量保存
        if(list != null && list.size() > 0){
            carResourcesMapper.batchInsert(list);
        }


    }

    @Override
    public List<CarResources> getCarResourceByCarId(Long carId) {
        return carResourcesMapper.getCarResourceByCarId(carId);
    }
}
